<?php

namespace app\manual\controller;

use think\Controller;
use think\Db;

class Doc extends Base
{

    public function index($type = 1)
    {
        $arr = ['/controller', '/controller/driver','/controller/passenger'];
        $dir = $arr[$type-1];
        $mk = 'api';
        return $this->getDoc($dir, $mk);
    }

    public function getDoc($dir, $mk)
    {
        $moudule = $mk;
        $controller = $dir;
        $conArr = explode('/', $controller);
        $arr = new \DirectoryIterator(APP_PATH . $moudule . $controller);
        $brr = [];
        $module_id = 2;
        $user_id = 1;
        foreach ($arr as $tt) {
            if ($arr->isDot()) {
                continue;
            }
            if ($arr->isDir()) {
                continue;
            }
            $serviceName = $tt->getBasename('.php');
            $controllerStr = str_replace('/', '\\', $moudule . $controller) . "\\";
            $rc = new \ReflectionClass("app\\" . $controllerStr . $serviceName);
            $comment = $rc->getDocComment();
            preg_match("/@name【(.*?)】/", $comment, $out);
            if (empty($out[1])) {
                continue;
            }
            $classname = $out[1];
            preg_match("/@baseurl【(.*?)】/", $comment, $out);
            $baseurl = $out[1];
            $methods = $rc->getMethods();
            foreach ($methods as $m) {
                $data = [];
                if ($m->class == "app\\" . $controllerStr . $serviceName) {
                    $bz = $m->getDocComment();
                    $bz = str_replace(['*', "\r", "\n", "\r\n", ' '], '', $bz);
                    preg_match("/@name【(.*?)】/", $bz, $out);
                    $data['title'] = $out[1];
                    if (empty($out[1])) {
                        continue;
                    }
                    $data['module_id'] = $module_id;
                    $data['user_id'] = $user_id;
                    preg_match("/@desc【(.*?)】/", $bz, $out);
                    $data['intro'] = $out[1];
                    preg_match("/@uri【(.*?)】/", $bz, $out);
                    $data['uri'] = $baseurl . "/" . $out[1];
                    preg_match("/@method【(.*?)】/", $bz, $out);
                    $data['method'] = $out[1];
                    preg_match("/@param【(.*?)】/", $bz, $out);
                    $param = $out[1];
                    $paramArr = explode('#', $param);
                    foreach ($paramArr as $p1) {
                        $p1Arr = explode('$', $p1);
                        $p11 = [];
                        $p11['name'] = $p1Arr[0];
                        $p11['title'] = $p1Arr[3];
                        $p11['type'] = $p1Arr[1];
                        $p11['default_value'] = $p1Arr[2];
                        $p11['is_required'] = isset($p1Arr[4]) ? 1 : 0;
                        $p11['method'] = 1;
                        $p11['user_id'] = $user_id;
                        $data['param'][] = $p11;
                    }
                    preg_match("/@return【(.*?)】/", $bz, $out);
                    $param = $out[1];
                    $paramArr = explode('#', $param);
                    foreach ($paramArr as $p1) {
                        $p1Arr = explode('$', $p1);
                        $p11 = [];
                        $p11['name'] = $p1Arr[0];
                        $p11['title'] = $p1Arr[3];
                        $p11['type'] = $p1Arr[1];
                        $p11['mock'] = $p1Arr[2];
                        $p11['default_value'] = $p1Arr[4];
                        $p11['is_required'] = 0;
                        $p11['method'] = 2;
                        $p11['user_id'] = $user_id;
                        $data['return'][] = $p11;
                    }
                }
                if (!empty($data)) {
                    $brr[] = $data;
                }
            }
        }
        $html = '<head><meta charset="UTF-8"><title>PCCX司机接口</title></head><style type="text/css">
        table
        {
            border-collapse: collapse;
            border: none;
            width: 200px;
        }
        td
        {
            border: solid #000 1px;
        }
    </style><table style="width:100%;" cellpadding="1" cellspacing="0" border="1" bordercolor="#000000">';
        $html .= '<tr style="background-color:rgb(223, 240, 216);"><th>接口名称</th><th>接口地址</th><th>请求方式</th><th>请求值</th><th>返回值</th></tr>';
        foreach ($brr as $v) {
            $html .= '<tr>';
            $html .= '<td>' . $v['title'] . '</td>';
            $html .= '<td>' . $v['uri'] . '</td>';
            $html .= '<td>' . $v['method'] . '</td>';
            $html .= '<td>';
            $html .= '<table style="width:100%;" cellpadding="1" cellspacing="0" border="1" bordercolor="#CCCCCC">';
            $html .= '<tr style="background-color:rgb(223, 240, 216);"><td>必传</td><td>参数名</td><td>描述</td><td>参考值</td></tr>';
            $jsonArr = [];
            foreach ($v['param'] as $v2) {//is_required
                $html .= '<tr>';
                if ($v2['is_required'] == 1) {
                    $html .= '<td>★</td>';
                } else {
                    $html .= '<td>☆</td>';
                }
                $table = '';
                $kk = json_decode($v2['default_value'], true);
                $table = is_array($kk) ? arrToTable($kk) : $v2['default_value'];
                if (is_array($kk)) {
                    $jsonArr['req'][$v2['name']] = $v2['default_value'];
                }
                $html .= '<td>' . $v2['name'] . '</td><td>' . $v2['title'] . '</td><td>' . $table . '</td>';
                $html .= '</tr>';
            }
            $html .= '</table>';
            $html .= '</td><td>';
            $html .= '<table style="width:100%;" cellpadding="1" cellspacing="0" border="1" bordercolor="#CCCCCC">';
            $html .= '<tr style="background-color:rgb(223, 240, 216);"><td>参数名</td><td>描述</td><td>参考值</td></tr>';
            foreach ($v['return'] as $v2) {
                $table = '';
                $kk = json_decode($v2['default_value'], true);
                $table = is_array($kk) ? arrToTable($kk) : $v2['default_value'];
                if (is_array($kk)) {
                    $jsonArr['ret'][$v2['name']] = $v2['default_value'];
                }
                $html .= '<tr>';
                $html .= '<td>' . $v2['name'] . '</td><td>' . $v2['title'] . '</td><td>' . $table . '</td>';
                $html .= '</tr>';

            }
            $html .= '</table>';
            $html .= '</td>';
            $html .= '</tr>';
            foreach ($jsonArr['req'] as $k3 => $v3) {
                $html .= '<tr style="background-color: rgba(169, 161, 158, 0.26);">';
                $html .= '<td>' . $k3 . '</td>';
                $html .= '<td colspan="4">' . $v3 . '</td>';
                $html .= '</tr>';
            }
            foreach ($jsonArr['ret'] as $k4 => $v4) {
                $html .= '<tr style="background-color: rgba(169, 161, 158, 0.26);">';
                $html .= '<td>' . $k4 . '</td>';
                $html .= '<td colspan="4">' . $v4 . '</td>';
                $html .= '</tr>';
            }
        }
        $html .= '</table>';
        return $html;
    }

    public function data()
    {
    	
        $dbserver = "rm-uf6qh06555b7wg24fo.mysql.rds.aliyuncs.com";
        $dbusername = "rebuild";
        $dbpassword = "XDG#jjcx&nacx";
        $database = 'car';
        $con = new \mysqli($dbserver, $dbusername, $dbpassword, $database);
        $rs = $con->query('show tables');
        $table = [];
        foreach ($rs as $key => $value) {
            $temp = [];
            $sql = 'SELECT * FROM ';
            $sql .= 'INFORMATION_SCHEMA.TABLES ';
            $sql .= 'WHERE ';
            $sql .= "table_name = '" . $value['Tables_in_car'] . "'  AND table_schema = '$database'";
            $rs2 = $con->query($sql);
            foreach ($rs2 as $key2 => $value2) {
                $temp['TABLE_NAME'] = $value2['TABLE_NAME'];
                $temp['TABLE_COMMENT'] = $value2['TABLE_COMMENT'];
            }
            $sql = 'SELECT * FROM ';
            $sql .= 'INFORMATION_SCHEMA.COLUMNS ';
            $sql .= 'WHERE ';
            $sql .= "table_name = '" . $value['Tables_in_car'] . "' AND table_schema = '$database'";
            $rs2 = $con->query($sql);
            foreach ($rs2 as $key2 => $value2) {
                $temp['COLUMN'][] = $value2;
            }
            $tables[] = $temp;
        }
        //循环所有表
        foreach ($tables AS $k => $v) {
            $html .= '<p><h2>' . $v['TABLE_COMMENT'] . '&nbsp;</h2>';
            $html .= '<table  width="100%" border="1" cellspacing="0" cellpadding="0" align="center">';
            $html .= '<caption>' . $v['TABLE_NAME'] . '</caption>';
            $html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th>
    <th>允许非空</th>
    <th>自动递增</th><th>备注</th></tr>';
            $html .= '';

            foreach ($v['COLUMN'] AS $f) {
            	
            	//alter
//            	!strpos($f['COLUMN_NAME'],'_id') && $sqls[] = "ALTER TABLE `{$v['TABLE_NAME']}` MODIFY COLUMN `{$f['COLUMN_NAME']}` {$f['COLUMN_TYPE']} COMMENT '".$this->alter[$v['TABLE_NAME']][$f['COLUMN_NAME']]."';";
            	
                $html .= '<tr><td class="c1">' . $f['COLUMN_NAME'] . '</td>';
                $html .= '<td class="c2">' . $f['COLUMN_TYPE'] . '</td>';
                $html .= '<td class="c3">&nbsp;' . $f['COLUMN_DEFAULT'] . '</td>';
                $html .= '<td class="c4">&nbsp;' . $f['IS_NULLABLE'] . '</td>';
                $html .= '<td class="c5">' . ($f['EXTRA'] == 'auto_increment' ? '是' : '&nbsp;') . '</td>';
                $html .= '<td class="c6">&nbsp;' . $f['COLUMN_COMMENT'] . '</td>';
                $html .= '</tr>';
            }
            $html .= '</tbody></table></p>';
        }
        
        //
        

//输出
        echo '<html>
<head>
<title>' . $title . '</title>
<style>
body,td,th {font-family:"宋体"; font-size:12px;}
table{border-collapse:collapse;border:1px solid #CCC;background:#efefef;}
table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
table th{text-align:left; font-weight:bold;height:26px; line-height:26px; font-size:12px; border:1px solid #CCC;}
table td{height:20px; font-size:12px; border:1px solid #CCC;background-color:#fff;}
.c1{ width: 120px;}
.c2{ width: 120px;}
.c3{ width: 70px;}
.c4{ width: 80px;}
.c5{ width: 80px;}
.c6{ width: 270px;}
</style>
</head>
<body>';
        echo '<h1>' . $title . '</h1>';
        echo $html;
        echo '</body></html>';
        
//        $sqls = implode(PHP_EOL,$sqls);
//		halt($sql);
//		file_put_contents(ROOT_PATH.'comment.sql',$sqls);

    }
    
    public function alter() {
		
		$file = ROOT_PATH.'db.export';
		$json = file_get_contents($file);
		$tables = json_decode($json,1)['tableList'];
		
		$tables = array_column($tables,null,'tableName');
		ksort($tables);
		
//		清空
		$alter = [];
		foreach($tables AS $table) {
			foreach($table['fieldList'] AS $field) {
//				$field['fieldDesc'] && $alter[]	= "ALTER TABLE `{$table['tableName']}` MODIFY COLUMN `{$field['fieldName']}` {$field['fieldType']} COMMENT '{$field['fieldDesc']}';"; 
				$alter[$table['tableName']][$field['fieldName']] = $field['fieldDesc'];
			}
			
		}
		
		$this->alter = $alter;
//		$sql = implode(PHP_EOL,$alter);
//		halt($sql);
//		file_put_contents(ROOT_PATH.'comment.sql',$sql);
		
	}

}
